package cn.yunhe.servlet;

import cn.yunhe.dao.IFoodDaoMapper;
import cn.yunhe.entity.merchantsentity.Food;
import cn.yunhe.entity.merchantsentity.Type;
import cn.yunhe.services.UpdateFoodService;
import cn.yunhe.util.SqlSessionFactoryUtil;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * 周依博
 * <p>
 * 菜品编辑的servlet提交
 *
 * @param
 * @return
 */
@WebServlet("/shangjia/UpdateFoodServlet")
public class UpdateFoodServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        SmartUpload sm = new SmartUpload();

        try {
            sm.initialize(this.getServletConfig(), request, response);
            sm.setCharset("UTF-8");
            sm.upload();
            sm.save("upfiles");
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }

            Request suRequest = sm.getRequest();
            int fid = Integer.parseInt(suRequest.getParameter("fid"));
            String name = suRequest.getParameter("name");
            String price = suRequest.getParameter("price");
            String pic = sm.getFiles().getFile(0).getFileName();
            int typeid = Integer.parseInt(suRequest.getParameter("typeid"));

            UpdateFoodService updateFoodService =new UpdateFoodService();
            Food food = updateFoodService.getNewsById(fid);
            food.setName(name);
            food.setPrice(price);
            food.setPic(pic);
            Type type = new Type();
            type.setTid(typeid);
            food.setType(type);
        response.setContentType("text/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
            if (updateFoodService.updateFood(food)>0){
                out.print("<script>alert('修改成功');location.href='product-list.jsp'</script>");
            }



    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
